// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import Heibaipei from "./Heibaipei";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Fruit extends cc.Component {


    start() {
        let minX = -this.node.parent.width / 2 + this.node.width / 2; //最小X坐标；
        let maxX = Math.abs(minX);
        let maxY = this.node.parent.height / 2 + this.node.height; //最小Y坐标；
        let x = Math.floor(this.random(-2, 2)) * 75;
        console.log(x);
        this.node.setPosition(new cc.Vec2(x, maxY));
    }

    random(min, max) {
        return Math.random() * (max - min) + min;
    }

    update(dt) {
        if (Heibaipei.running) {
            this.move();
        }else{
            this.node.destroy();
        }
    }

    move() {
        this.node.y-=  2;
    }

    onCollisionEnter(other, self) {
        this.node.destroy();
    }
}
